package com.hzo.great.leisuregreat.views;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.base.BaseActivity;
import com.hzo.great.leisuregreat.constants.UrlConstant;
import com.hzo.great.leisuregreat.dialog.LoadingDialog;
import com.hzo.great.leisuregreat.utils.ImagexUtils;
import com.hzo.great.leisuregreat.utils.MyStringCallback;
import com.hzo.great.leisuregreat.utils.RequestSever;
import com.hzo.great.leisuregreat.utils.SharedPreferencesUtils;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderActivity extends BaseActivity {

    ImageView back;

    TextView all, waitPay, waitSend, waitReceive, waitEvaluation;

    PullToRefreshLayout pulltorefresh;

    RecyclerView recyclerView;

    List<Map<String,String>> listmap = new ArrayList<>();
    LinearLayout empty_layout;
    RecyclerViewAdapter recycleradapter;

    TextView[] textArr;

    int pageCurrent = 1, pageSize = 5, status = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fullScreen(this, true);
        setContentView(R.layout.order_activity);
        initView();
        status = getIntent().getIntExtra("status",-1);
        if (status == -1){
            changeNavBg(0);
        }else {
            changeNavBg(status);
        }
        getList(1);
    }

    private void initView() {

        back = (ImageView) findViewById(R.id.back);

        all = (TextView) findViewById(R.id.order_all);
        waitPay = (TextView) findViewById(R.id.order_waitPay);
        waitSend = (TextView) findViewById(R.id.order_waitSend);
        waitReceive = (TextView) findViewById(R.id.order_waitReceive);
        waitEvaluation = (TextView) findViewById(R.id.order_waitEvaluation);

        pulltorefresh = (PullToRefreshLayout) findViewById(R.id.pulltorefresh);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        empty_layout = (LinearLayout) findViewById(R.id.empty_layout);

        textArr = new TextView[]{all,waitPay,waitSend,waitReceive,waitEvaluation};

        pulltorefresh.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                pageCurrent = 1;
                getList(1);
            }

            @Override
            public void loadMore() {
                pageCurrent++;
                getList(2);
            }
        });

        recycleradapter = new RecyclerViewAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setAdapter(recycleradapter);

        back.setOnClickListener(this);
        all.setOnClickListener(this);
        waitPay.setOnClickListener(this);
        waitSend.setOnClickListener(this);
        waitReceive.setOnClickListener(this);
        waitEvaluation.setOnClickListener(this);

    }


    private void getList(final int tag) {
        Map<String, Object> map = new HashMap<>();
        map.put("access_token", SharedPreferencesUtils.getString("access_token",""));
        map.put("custermerId",SharedPreferencesUtils.getString("customerId",""));
        map.put("status",status);
        map.put("pageCurrent",pageCurrent);
        map.put("pageSize",pageSize);
        LoadingDialog.getInstance(this).showLoadDialog("");
        RequestSever.post(UrlConstant.ORDERLIST_URL,map,new MyStringCallback<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LoadingDialog.closeLoadDialog();
                JSONObject jsonObject = JSON.parseObject(result);
                switch (jsonObject.getIntValue("code")){
                    case 200:
                        List<Map<String,String>> list = JSON.parseObject(String.valueOf(jsonObject.getJSONArray("data")),new TypeReference<List<Map<String, String>>>(){});
                        refreshList(tag,list);
                        break;

                    default:
                        ShortToast(jsonObject.getString("message"));
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LoadingDialog.closeLoadDialog();
            }
        });
    }

    private void refreshList(int tag, List<Map<String, String>> list) {
        switch (tag) {
            case 1:
                listmap.clear();
                pulltorefresh.finishRefresh();
                if (list == null || list.size() <= 0) {
                    empty_layout.setVisibility(View.VISIBLE);
                    pulltorefresh.setVisibility(View.GONE);
                    pulltorefresh.setCanLoadMore(false);
                    return;
                }
                listmap.addAll(list);
                empty_layout.setVisibility(View.GONE);
                pulltorefresh.setVisibility(View.VISIBLE);
                recycleradapter.notifyDataSetChanged();
                break;
            case 2:
                if (list != null) {
                    listmap.addAll(list);
                    recycleradapter.notifyDataSetChanged();
                }
                pulltorefresh.finishLoadMore();
                break;
        }
    }

    private void ShortToast(String s) {
        Toast.makeText(OrderActivity.this,s,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.order_all:
                status = -1;
                pageCurrent = 1;
                getList(1);
                changeNavBg(0);
                break;
            case R.id.order_waitPay:
                status = 1;
                pageCurrent = 1;
                getList(1);
                changeNavBg(1);
                break;
            case R.id.order_waitSend:
                status = 2;
                pageCurrent = 1;
                getList(1);
                changeNavBg(2);
                break;
            case R.id.order_waitReceive:
                status = 3;
                pageCurrent = 1;
                getList(1);
                changeNavBg(3);
                break;
            case R.id.order_waitEvaluation:
                status = 4;
                pageCurrent = 1;
                getList(1);
                changeNavBg(4);
                break;

        }
    }


    class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
        @NonNull
        @Override
        public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(OrderActivity.this).inflate(R.layout.item_order, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

            String status = listmap.get(position).get("Status");
            switch (status){
                case "0":
                    holder.state.setText("待提交");
                    break;
                case "1":
                    holder.state.setText("待付款");
                    break;
                case "2":
                    holder.state.setText("等待发货");
                    break;
                case "3":
                    holder.state.setText("已发货");
                    break;
                case "4":
                    holder.state.setText("交易成功");
                    break;
            }
            holder.time.setText(listmap.get(position).get("CreateTime"));
            holder.contact.setText(listmap.get(position).get("Title") + listmap.get(position).get("NetType"));
            holder.jine.setText("¥ " + listmap.get(position).get("CommodityPrice"));
            holder.count.setText("x" + listmap.get(position).get("rownum"));
            holder.allCount.setText("共" + listmap.get(position).get("rownum") + "件商品 合计:");
            holder.allJine.setText("¥"+ Float.parseFloat(listmap.get(position).get("CommodityPrice"))* Float.parseFloat(listmap.get(position).get("rownum")));
            holder.yunfei.setText(" (含运费￥" + listmap.get(position).get("DeliverPrice") + "）");

            ImagexUtils.setImg(holder.image,listmap.get(position).get("TargetPhone"));
            switch (status){
                case "0":
                    holder.cancel.setVisibility(View.VISIBLE);
                    holder.submit.setVisibility(View.VISIBLE);
                    break;
                case "1":
                    holder.yunfei.setVisibility(View.GONE);
                    holder.pay.setVisibility(View.VISIBLE);
                    break;
                case "2":
                    holder.cancel.setVisibility(View.VISIBLE);
                    holder.cuicu.setVisibility(View.VISIBLE);
                    break;
                case "3":
                    holder.confirm.setVisibility(View.VISIBLE);
                    holder.check.setVisibility(View.VISIBLE);
                    break;
                case "4":
                    holder.delete.setVisibility(View.VISIBLE);
                    holder.evaluation.setVisibility(View.VISIBLE);
                    break;
            }

            holder.pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("支付");
                }
            });

            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("删除");
                }
            });

            holder.cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("取消");
                }
            });

            holder.check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("查看");
                }
            });

            holder.evaluation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("评价");
                }
            });

            holder.confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("确认");
                }
            });

            holder.cuicu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShortToast("催促");
                }
            });
        }

        @Override
        public int getItemCount() {
            return listmap.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView time, state, contact, jine, count, allCount, allJine, yunfei, pay, delete, cancel, check, evaluation, confirm, cuicu, submit;

            ImageView image;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);

                time = (TextView) itemView.findViewById(R.id.time);
                state = (TextView) itemView.findViewById(R.id.state);
                contact = (TextView) itemView.findViewById(R.id.contact);
                count = (TextView) itemView.findViewById(R.id.count);
                allCount = (TextView) itemView.findViewById(R.id.allCount);
                allJine = (TextView) itemView.findViewById(R.id.allJine);
                yunfei = (TextView) itemView.findViewById(R.id.yunfei);
                pay = (TextView) itemView.findViewById(R.id.pay);
                delete = (TextView) itemView.findViewById(R.id.delete);
                cancel = (TextView) itemView.findViewById(R.id.cancel);
                check = (TextView) itemView.findViewById(R.id.check);
                evaluation = (TextView) itemView.findViewById(R.id.evaluation);
                confirm = (TextView) itemView.findViewById(R.id.confirm);
                cuicu = (TextView) itemView.findViewById(R.id.cuicu);
                jine = (TextView) itemView.findViewById(R.id.jine);
                submit = (TextView) itemView.findViewById(R.id.submit);

                image = (ImageView) itemView.findViewById(R.id.image);
            }
        }
    }


    private void changeNavBg(int index) {
        for (int i = 0; i < 5; i++) {
            if (index == i) {
                textArr[i].setTextColor(Color.parseColor("#4c7bfd"));
                textArr[i].setClickable(false);
            } else {
                textArr[i].setTextColor(Color.parseColor("#333333"));
                textArr[i].setClickable(true);

            }
        }
    }
}
